package com.dawn.module.hospital.controller.admin.medicalcopy;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.*;

import com.dawn.module.hospital.controller.admin.medicalcopy.vo.*;
import com.dawn.module.hospital.dal.dataobject.medicalcopy.MedicalCopyDO;
import com.dawn.module.hospital.service.medicalcopy.MedicalCopyService;

@Tag(name = "管理后台 - 病案复印")
@RestController
@RequestMapping("/hospital/medical-copy")
@Validated
public class MedicalCopyController {

    @Resource
    private MedicalCopyService medicalCopyService;

    @PostMapping("/create")
    @Operation(summary = "创建病案复印")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:create')")
    public CommonResult<Long> createMedicalCopy(@Valid @RequestBody MedicalCopySaveReqVO createReqVO) {
        return success(medicalCopyService.createMedicalCopy(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新病案复印")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:update')")
    public CommonResult<Boolean> updateMedicalCopy(@Valid @RequestBody MedicalCopySaveReqVO updateReqVO) {
        medicalCopyService.updateMedicalCopy(updateReqVO);
        return success(true);
    }

    @PostMapping("/examine")
    @Operation(summary = "审核病案复印")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:examine')")
    public CommonResult<Boolean> examineMedicalCopy(@Valid @RequestBody MedicalCopyExamineReqVO examineReqVO) {
        medicalCopyService.examineMedicalCopy(examineReqVO);
        return success(true);
    }

    @PostMapping("/send")
    @Operation(summary = "发货/取件")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:send')")
    public CommonResult<Boolean> sendMedicalCopy(@Valid @RequestBody MedicalCopyEndReqVO endReqVO) {
        medicalCopyService.sendMedicalCopy(endReqVO);
        return success(true);
    }

    @PostMapping("/end")
    @Operation(summary = "完成病案复印")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:end')")
    public CommonResult<Boolean> endMedicalCopy(@Valid @RequestBody MedicalCopyEndReqVO endReqVO) {
        medicalCopyService.endMedicalCopy(endReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除病案复印")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:delete')")
    public CommonResult<Boolean> deleteMedicalCopy(@RequestParam("id") Long id) {
        medicalCopyService.deleteMedicalCopy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得病案复印")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:query')")
    public CommonResult<MedicalCopyRespVO> getMedicalCopy(@RequestParam("id") Long id) {
        MedicalCopyDO medicalCopy = medicalCopyService.getMedicalCopy(id);
        return success(BeanUtils.toBean(medicalCopy, MedicalCopyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得病案复印分页")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:query')")
    public CommonResult<PageResult<MedicalCopyRespVO>> getMedicalCopyPage(@Valid MedicalCopyPageReqVO pageReqVO) {
        PageResult<MedicalCopyDO> pageResult = medicalCopyService.getMedicalCopyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MedicalCopyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出病案复印 Excel")
    @PreAuthorize("@ss.hasPermission('hospital:medical-copy:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMedicalCopyExcel(@Valid MedicalCopyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MedicalCopyDO> list = medicalCopyService.getMedicalCopyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "病案复印.xls", "数据", MedicalCopyRespVO.class,
                        BeanUtils.toBean(list, MedicalCopyRespVO.class));
    }

}